<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    width="800px"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="auto" :rules="rules">      

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="标题" prop="title" class="person">
            <el-input
              v-model="form.title"
              placeholder="标题"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发布医生" prop="userId" class="person">
            <el-input
              v-if="type === 'edit'"
              :value="form.doctorName"
              readonly
            ></el-input>
            <el-select v-else v-model="form.userId" placeholder="请选择">
              <el-option
                v-for="item in doctors"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="回答" prop="content" class="person">
            <el-input
              v-model="form.content"
              placeholder="回答"
              :rows="6"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import BindDialog from '@/components/bindDialog/index.vue'
import { dialogFrame } from './frameData'
import { modifyQuestionAnswer, addQuestionAnswer } from '@/api/qa'
import { getDoctorList } from '@/api/doctor';

const markForm = () => {
  return {
    title: "",
    content: "",
    doctorName: "",
    doctorId: ""
  }
}

export default {
  name: "",
  components: {
    BindDialog
  },
  data() {
    return {
      doctors: [],
      rules: {
        title: [{ required: true, message: '标题是必填项', trigger: 'blur' }],
        content: [{ required: true, message: '回答是必填项', trigger: 'blur' }]
      },
      config: {
        edit: {
          func: modifyQuestionAnswer,
          title: "编辑"
        },
        new: {
          func: addQuestionAnswer,
          title: "新增"
        }
      },
      form: markForm(),
      visible: false,
      inputValue: "",
      inputVisible: false,
      dialogFrame,
      query: '',
      loading: false,
      options: []
    };
  },
  methods: {
    queryDoctorAsync() {
      getDoctorList({
          pageNum: 1,
          pageSize: 500,
      }).then(res => {
        if(res.rows && res.rows.length >= 0){
          this.doctors = res.rows.filter(item => item.name).map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
        }
      })
    },

    showDialog(row) {
      this.reset();
      this.queryDoctorAsync()
      const isEdit = !!row;
      this.visible = true;
      this.type = isEdit ? "edit" : "new";
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      if (isEdit) {
        this.form = Object.assign(this.form, row);
        if(row.userVo){
          this.form.doctorName = row.userVo.name;
        }
      }
      this.title = this.config[this.type].title;
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.visible = false;
          const data = { ...this.form }

          if(this.type === "new"){
            delete data.id;
          }

          delete data.doctorName
          delete data.createTime

          this.config[this.type]?.func(data).then(() => {
            this.$modal.msgSuccess("提交成功");
            this.visible = false;
            this.$emit("getTableList");
          });
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.visible = false;
      this.fileList = [];
    },

    // 表单重置
    reset() {
      this.form = markForm();
      this.$forceUpdate();
      this.fileList = [];
    },

  },
};
</script>
<style scoped lang="scss">

</style>
